/*
 * This file is part of Smart Route for S60 3rd Edition terminals
 * 
 * Copyright (C) 2006  Harri Salminen

 * This program free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 *(at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
 

#include <aknviewappui.h>
#include <eikmenub.h>
#include <avkon.hrh>
#include <akncontext.h>
#include <akntitle.h>
#include <stringloader.h>
#include <barsread.h>
#include <eikbtgpc.h>

#include "SmartRoute.rsg"
#include "SmartRoute.hrh"
#include "SettingItemListView.h"
#include "SettingItemList.hrh"
#include "SettingItemList.h"

CSmartRouteSettingItemListView::CSmartRouteSettingItemListView()
	{
	}
	
CSmartRouteSettingItemListView::~CSmartRouteSettingItemListView()
	{
	}

CSmartRouteSettingItemListView* CSmartRouteSettingItemListView::NewL()
	{
	CSmartRouteSettingItemListView* self = CSmartRouteSettingItemListView::NewLC();
	CleanupStack::Pop( self );
	return self;
	}

CSmartRouteSettingItemListView* CSmartRouteSettingItemListView::NewLC()
	{
	CSmartRouteSettingItemListView* self = new (ELeave) CSmartRouteSettingItemListView();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}
 
void CSmartRouteSettingItemListView::ConstructL()
	{
	BaseConstructL(SMART_ROUTE_SETTING_ITEM_LIST_VIEW);
	}

TUid CSmartRouteSettingItemListView::Id() const
	{
	return TUid::Uid(ESettingsListViewId);
	}

void CSmartRouteSettingItemListView::HandleCommandL( TInt aCommand )
	{   
	switch ( aCommand )
		{
		case ESmartRouteSettingChangeSelectedItem:
			iSmartRouteSettingItemList->ChangeSelectedItemL();
			break;
		case EAknSoftkeyBack:
			iSmartRouteSettingItemList->StoreSettingsL();
			AppUi()->ActivateLocalViewL(TUid::Uid(EMainScreenViewId));
			break;
		default:
			AppUi()->HandleCommandL(aCommand);
		}
	}

void CSmartRouteSettingItemListView::DoActivateL(
		const TVwsViewId& /*aPrevViewId*/,
		TUid /*aCustomMessageId*/,
		const TDesC8& /*aCustomMessage*/ )
	{
	SetupStatusPaneL();
	
	CEikButtonGroupContainer *cba = AppUi()->Cba();
	if ( cba ) 
		{
		cba->MakeVisible( EFalse );
		}
	
	if ( !iSmartRouteSettingItemList )
		{
		iSmartRouteSettingItemList = new (ELeave) CSmartRouteSettingItemList(this);
		iSmartRouteSettingItemList->SetMopParent( this );
		iSmartRouteSettingItemList->ConstructFromResourceL( R_SMART_ROUTE_SETTING_ITEM_LIST_SMART_ROUTE_SETTING_ITEM_LIST );
		iSmartRouteSettingItemList->ActivateL();
		iSmartRouteSettingItemList->LoadSettingValuesL();
		iSmartRouteSettingItemList->LoadSettingsL();
		AppUi()->AddToStackL( *this, iSmartRouteSettingItemList );
		} 	
	}


void CSmartRouteSettingItemListView::DoDeactivate()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	CleanupStatusPane();
	
	CEikButtonGroupContainer *cba = AppUi()->Cba();
	if ( cba ) 
		{
		cba->MakeVisible( ETrue );
		cba->DrawDeferred();
		}
	
	if ( iSmartRouteSettingItemList )
		{
		AppUi()->RemoveFromStack( iSmartRouteSettingItemList );
		delete iSmartRouteSettingItemList;
		iSmartRouteSettingItemList = NULL;
		}	
	}

void CSmartRouteSettingItemListView::SetupStatusPaneL()
	{
	// reset the context pane
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext = 
		StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
		{
		CAknContextPane* context = static_cast< CAknContextPane* > ( 
			StatusPane()->ControlL( contextPaneUid ) );
		context->SetPictureToDefaultL();
		}
	
	// setup the title pane
	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle = 
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
		{
		CAknTitlePane* title = static_cast< CAknTitlePane* >( 
			StatusPane()->ControlL( titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_SMART_ROUTE_SETTING_ITEM_LIST_TITLE_RESOURCE );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
				
	}

void CSmartRouteSettingItemListView::CleanupStatusPane()
	{
	}

void CSmartRouteSettingItemListView::HandleStatusPaneSizeChange()
	{
	CAknView::HandleStatusPaneSizeChange();
	TInt result;
	TRAP( result, SetupStatusPaneL() ); 
	}
	

TBool CSmartRouteSettingItemListView::HandleChangeSelectedSettingItemL( TInt aCommand )
	{
	iSmartRouteSettingItemList->ChangeSelectedItemL();
	return ETrue;
	}
				
